package com.dituhui.gps.local;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.dituhui.gps.vo.GPSPoint;
import com.dituhui.tool.io.Constants;
import com.dituhui.tool.io.ESTool;
import com.dituhui.tool.util.StringUtil;

import net.sf.json.JSONArray;

/**
 * 统计用户访问数
 * 
 * @author liweigu
 *
 */
public class UserStatisticTool {
	private static String HistoryESUrl = Constants.getValue("HistoryESUrl", "gps_config.properties");
	private static String HistoryES_IndexPrefix = Constants.getValue("HistoryES_IndexPrefix", "gps_config.properties");
	protected static Logger LOGGER = Logger.getLogger(UserStatisticTool.class);

	// ES历史的字段名
	private static String INFOS_FIELD = "infos";
	private static String AccountID_FIELD = "accountID";

	public void queryByDay() {
		String index = null;
		// 查询全部数据
		String[] queryColumnNames = null;
		String[] queryColumnValues = null;
		List<Map<String, Object>> mapList = ESTool.queryToMapList(index, queryColumnNames, queryColumnValues, HistoryESUrl);
		for (Map<String, Object> map : mapList) {
			Map<String, String> sourceMap = (Map<String, String>) map.get("_source");
			String currentIndex = (String) map.get("_index");
			// LOGGER.info("currentIndex=" + currentIndex);
			String date = currentIndex.replaceAll(HistoryES_IndexPrefix, "");
			// LOGGER.info("date=" + date);
			String infos = sourceMap.get(INFOS_FIELD);
			// LOGGER.info("infos=" + infos);
			if (infos.contains(StringUtil.DoubleUnderline)) {
				// 分隔符格式
				String[] gpsPointArray = infos.split(StringUtil.DoubleUnderline);
				for (String str : gpsPointArray) {
					GPSPoint gpsPoint = GPSPoint.parseCSVString(str);
				}
			} else {
				// JSON格式。TODO: 验证是否会进入此分支。
				JSONArray gpsPointArray = JSONArray.fromObject(infos);
				// System.out.println(gpsPoints);
				for (int i = 0; i < gpsPointArray.size(); i++) {
					String str = gpsPointArray.getString(i);
					// System.out.println(str);
					GPSPoint gpsPoint = GPSPoint.parseCSVString(str);
				}
			}
			// LOGGER.info("(getGPSLineFromHistoryES)infos=" + infos);
		}
	}

	public static void main(String[] args) {

	}

}
